<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="44px"
    >
      <!--      <el-form-item label="数据源名称" prop="datasourceName">
        <el-input
          v-model="queryParams.datasourceName"
          placeholder="请输入数据源名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>-->
      <el-form-item label="Name" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="Please input the name"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <!--      <el-form-item label="年龄" prop="age">
        <el-input
          v-model="queryParams.age"
          placeholder="请输入年龄"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="临床诊断类型" prop="clinicalDiagnosisType">
        <el-select
          v-model="queryParams.clinicalDiagnosisType"
          placeholder="请选择临床诊断类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否可见生殖细胞" prop="germCellsVisible">
        <el-select
          v-model="queryParams.germCellsVisible"
          placeholder="请选择是否可见生殖细胞"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="染色体核型" prop="karyotype">
        <el-select
          v-model="queryParams.karyotype"
          placeholder="请选择染色体核型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否有遗传家系" prop="isGeneticFamily">
        <el-select
          v-model="queryParams.isGeneticFamily"
          placeholder="请选择是否有遗传家系"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="数据类型" prop="dataType">
        <el-select
          v-model="queryParams.dataType"
          placeholder="请选择数据类型"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="样本数据是否已上传" prop="isDataUploaded">
        <el-select
          v-model="queryParams.isDataUploaded"
          placeholder="请选择样本数据是否已上传"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="数据文件路径" prop="dataPath">
        <el-input
          v-model="queryParams.dataPath"
          placeholder="请输入数据文件路径"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="数据文件列表" prop="files">
        <el-input
          v-model="queryParams.files"
          placeholder="请输入数据文件列表"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="数据大小" prop="dataSize">
        <el-input
          v-model="queryParams.dataSize"
          placeholder="请输入数据大小"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="细胞比例详情" prop="cellRatios">
        <el-input
          v-model="queryParams.cellRatios"
          placeholder="请输入细胞比例详情"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="细胞数量" prop="cellCount">
        <el-input
          v-model="queryParams.cellCount"
          placeholder="请输入细胞数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="基因数量" prop="geneCount">
        <el-input
          v-model="queryParams.geneCount"
          placeholder="请输入基因数量"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="备注" prop="notes">
        <el-input
          v-model="queryParams.notes"
          placeholder="请输入备注"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>-->
      <el-form-item>
        <el-button @click="handleQuery">
          <Icon icon="ep:search" class="mr-5px" />
          Search
        </el-button>
        <el-button @click="resetQuery">
          <Icon icon="ep:refresh" class="mr-5px" />
          Reset
        </el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['sca:datasource:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" />
          Add
        </el-button>
        <!--        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['sca:datasource:export']"
        >
          <Icon icon="ep:download" class="mr-5px" />
          导出
        </el-button>-->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" border>
      <el-table-column label="ID" align="center" prop="id" width="60px" />
      <el-table-column
        label="Data Source Name"
        align="center"
        prop="datasourceName"
        width="220px"
      />
      <el-table-column label="Name" align="center" prop="name" width="120px" />
      <el-table-column label="Age" align="center" prop="age" />
      <el-table-column
        label="Clinical Diagnosis Type"
        align="center"
        prop="clinicalDiagnosisType"
        width="120px"
      />
      <el-table-column
        label="Visibility of Germ Cells"
        align="center"
        prop="germCellsVisible"
        width="120px"
      >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.germCellsVisible" />
        </template>
      </el-table-column>
      <el-table-column label="Chromosome Karyotype" align="center" prop="karyotype" width="120px" />
      <el-table-column
        label="Presence of Genetic Pedigree"
        align="center"
        prop="isGeneticFamily"
        width="130px"
      >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.isGeneticFamily" />
        </template>
      </el-table-column>
      <el-table-column label="Data Type" align="center" prop="dataType" width="200px" />
      <el-table-column
        label="Sample File Uploaded"
        align="center"
        prop="isDataUploaded"
        width="120px"
      >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.isDataUploaded" />
        </template>
      </el-table-column>
      <!--      <el-table-column label="数据文件路径" align="center" prop="dataPath" width="100px" />-->
      <!--      <el-table-column label="数据文件列表" align="center" prop="files" />-->
      <!--      <el-table-column label="数据大小" align="center" prop="dataSize" />-->
      <!--      <el-table-column label="细胞比例详情" align="center" prop="cellRatios" />-->
      <el-table-column label="Cell Count" align="center" prop="cellCount" width="100px" />
      <el-table-column label="Gene Count" align="center" prop="geneCount" width="100px" />
      <el-table-column label="Remarks" align="center" prop="notes" width="100px" />
      <el-table-column
        label="CreateAt"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column
        label="Operations"
        align="center"
        fixed="right"
        width="120px"
        v-hasPermi="['sca:datasource:update']"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['sca:datasource:update']"
          >
            Edit
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['sca:datasource:delete']"
          >
            Delete
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <DatasourceForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts" name="Datasource">
import { dateFormatter } from '@/utils/formatTime'
import { DICT_TYPE } from '@/utils/dict'
import download from '@/utils/download'
import * as DatasourceApi from '@/api/sca/datasource'
import DatasourceForm from './DatasourceForm.vue'

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  datasourceName: null,
  name: null,
  age: null,
  clinicalDiagnosisType: null,
  germCellsVisible: null,
  karyotype: null,
  isGeneticFamily: null,
  dataType: null,
  isDataUploaded: null,
  dataPath: null,
  files: null,
  dataSize: null,
  cellRatios: null,
  cellCount: null,
  geneCount: null,
  notes: null,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await DatasourceApi.getDatasourcePage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await DatasourceApi.deleteDatasource(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await DatasourceApi.exportDatasource(queryParams)
    download.excel(data, '数据源.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/

onMounted(() => {
  getList()
})
</script>
